package com.echo.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.activeandroid.query.Select;
import com.echo.activity.R;
import com.echo.activity.SportsActivity;
import com.echo.model.DayData;
import com.echo.model.Sport;
import com.echo.service.StepDetector;
import com.echo.util.DateUtil;
import com.echo.util.PasswordUtil;
import com.echo.util.TimeUtil;

import org.json.JSONObject;

import java.util.Calendar;
import java.util.List;

/**
 * Created by EchoGe on 2016/5/13.
 */
public class WeekDayDataFragment extends Fragment{
    
    private View mView;

    private TextView mCalories,mStep,mTime,mDistance;

    private Calendar mCalendar;

    private DayData mDayData;

    public WeekDayDataFragment(){

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(R.layout.fragment_day_data, container, false);
            initView(mView);
            load();
        }

        return mView;
    }

    private void initView(View mView) {
        mCalories= (TextView) mView.findViewById(R.id.day_calories);
        mStep= (TextView) mView.findViewById(R.id.day_step);
        mTime= (TextView) mView.findViewById(R.id.day_time);
        mDistance= (TextView) mView.findViewById(R.id.day_distance);
    }

    public void update(){
        load();
    }

    private void load(){
        mDayData=new Select()
                .from(DayData.class)
                .where("date=?", DateUtil.getDateStr(mCalendar))
                .orderBy("RANDOM()")
                .executeSingle();
        if(mDayData==null) {
            mDayData=new DayData();
            mDayData.setDate(DateUtil.getDateStr(mCalendar));
        }else{
            List<Sport> sportList=new Select().from(Sport.class).where("date=?",mDayData.getDate()).execute();
            if(sportList!=null) {
                int time=0;
                int calories=0;
                int distance=0;
                for (int i = 0; i < sportList.size(); i++) {
                    time+=sportList.get(i).getTime();
                    calories+=sportList.get(i).getCalories();
                    distance+=sportList.get(i).getDistance();
                }
                mDayData.setTime(time);
                mDayData.setCalories(calories);
                mDayData.setDistance(distance);
            }
        }
        if(DateUtil.getDateStr(mCalendar).equals(DateUtil.getDateStr(Calendar.getInstance()))){
            mDayData.setSteps(StepDetector.CURRENT_SETP);
        }
        mCalories.setText(String.valueOf(mDayData.getCalories()));
        mStep.setText(String.valueOf(mDayData.getSteps()));
        mTime.setText(TimeUtil.secondsToStr(mDayData.getTime()));
        mDistance.setText(String.valueOf(mDayData.getDistance()));
        mDayData.save();
    }

    public void setCalendar(Calendar calendar){
        this.mCalendar=calendar;
    }
}
